package com.chenzhou.controller;

import com.chenzhou.entity.GoodsEntity;
import com.chenzhou.entity.GoodsImage;
import com.chenzhou.service.GoodsImageService;
import com.chenzhou.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/goods_image")
public class GoodsImagesController {

    @Autowired
    private GoodsImageService service;

    @Autowired
    private GoodsService goodsService;

    /**
     * 显示商品信息
     * @param request
     * @return
     */
    @RequestMapping("/show_goods_image")
    public String queryAllGoods(HttpServletRequest request){
        List<GoodsEntity> list = goodsService.queryAllGoodsEntity();
        request.setAttribute("glist",list);
        request.setAttribute("menu","goods_image");
        return "goods_images/show_goods_image";
    }

    /**
     * 根据商品id显示商品信息和图片
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/show_goodsInfo")
    public String show_goods_image(HttpServletRequest request,Integer id){
        GoodsEntity goodsEntity = goodsService.queryGoodsEntityOne(id);
        List<GoodsImage> list = service.queryGoodImageByGoodsID(id);
        request.setAttribute("goods",goodsEntity);
        request.setAttribute("image_list",list);
        request.setAttribute("menu","goods_image");
        return "goods_images/goods_image_upload";
    }

    /***
     * 上传图片
     * @param request
     * @param goods_id
     * @param picture
     * @return
     */
    @RequestMapping("/upload_image")
    public String upload_image(HttpServletRequest request,Integer goods_id,MultipartFile picture){

        String basePath = "D:\\image_server\\nginx-1.19.0\\html\\img\\";//物理位置

        //123.jpg
        String fileName = picture.getOriginalFilename();
        String prefix_name = UUID.randomUUID().toString();//唯一编码
        String suffix_name = fileName.substring(fileName.lastIndexOf('.'));
        File file = new File(basePath+prefix_name+suffix_name);
        try {
            picture.transferTo(file);//讲文件流写入文件
        } catch (IOException e) {
            e.printStackTrace();
        }
        GoodsImage goodsImage = new GoodsImage();
        goodsImage.setFilename(basePath+prefix_name+suffix_name);//物理路径
        goodsImage.setFile_url("http://localhost/img/"+prefix_name+suffix_name);//url路径
        goodsImage.setFile_pix(suffix_name);
        goodsImage.setGood_id(goods_id);
        goodsImage.setMarkup("图片上传成功！！！！");

        int a = service.add_GoodIamge(goodsImage);
        if(a>0) {
            return this.show_goods_image(request,goods_id);
        }else {
            return "forward:/error.jsp";
        }
    }

    /***
     * 删除图片
     * @param request
     * @param image_id
     * @return
     */
    @RequestMapping("/delete_image")
    public String delete_goodsIamge(HttpServletRequest request,Integer image_id){
        GoodsImage goodsImage = service.queryGoodsImageByID(image_id);
        int a = service.delete_goodsImage(image_id);//删除数据库中图片信息
        service.deleteFile(goodsImage.getFilename());//删除nginx中的图片
        if(a>0) {
            return this.show_goods_image(request,goodsImage.getGood_id());
        }else {
            return "forward:/error.jsp";
        }
    }
}
